<template>
  <div class="mod-config">
    <el-form inline @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.returnBillId"></el-input>
      </el-form-item>
      <el-form-item label="运单单号">
        <el-input v-model="dataForm.transportId"></el-input>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.createUsername"></el-input>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input  v-model="dataForm.supplyName" placeholder="请选择">
          <el-button slot="append" @click="chooseSupply">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="退货部门">
        <el-select v-model="dataForm.deptId">
          <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发出仓库">
        <el-select v-model="dataForm.inStockId">
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.auditStatus">
          <el-option v-for="item in auditStatusOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核人">
        <el-input v-model="dataForm.auditUsername"></el-input>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-select v-model="dataForm.ctgId" @change="changeCtgId">
          <el-option v-for="item in ctgOptions" :value="item.value" :label="item.name" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.memo"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker
              v-model="dataForm.createTimeStart"
              style="width: 100%;"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker
              v-model="dataForm.createTimeEnd"
              style="width: 100%;"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="是否显示退货商品数量">
        <el-checkbox v-model="dataForm.countShowFlag">显示</el-checkbox>
      </el-form-item>
      <div v-if="this.dataForm.countShowFlag">
        <el-divider content-position="left">统计商品数量补充条件</el-divider>
        <el-form-item label="退货商品品种" prop="brandName">
          <el-input v-model="dataForm.brandName" readonly="readonly">
            <el-button slot="append" @click="getBrandList">选择</el-button>
          </el-input>
        </el-form-item>
      </div>

      <el-form-item>
        <el-button icon="el-icon-search" type="primary" plain @click="searchHandle">查询</el-button>
        <el-button icon="el-icon-search" type="info" plain @click="clearSearchForm">清除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          v-if="permissions.eims_returnmanagement_add"
          icon="el-icon-plus"
          type="primary"
          @click="addreturnGoodsOrder()"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <div  v-if="this.dataForm.countShowFlag">
      <el-row style="margin-bottom: 15px;">
          退货商品数量总计：{{ this.sum }}
      </el-row>
    </div>

    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
        <el-table-column prop="returnBillId" label="单据编号" width="140"></el-table-column>
        <el-table-column prop="transportId" label="运单单号" width="140"></el-table-column>
        <el-table-column prop="createTime" label="单据日期"></el-table-column>
        <el-table-column prop="deptName" label="退货部门" width="100"></el-table-column>
        <el-table-column prop="outStockName" label="发出仓库" width="100"></el-table-column>
        <el-table-column prop="auditStatus" label="审核状态" width="80" :formatter="auditStatusFormatter"></el-table-column>
        <el-table-column prop="createUsername" label="制单人" width="80"></el-table-column>
        <el-table-column prop="auditUsername" label="审核人" width="80"></el-table-column>
        <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="210" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-collection"
              @click="returnGoodsDetail(scope.row.id)"
            >详情</el-button>
            <el-button
              type="text"
              icon="el-icon-collection"
              @click="auditInfo(scope.row.id,scope.row.returnBillId)"
              :disabled="scope.row.auditStatus == 1"
            >审核</el-button>
            <el-button v-if="permissions.eims_returnmanagement_edit" :disabled="scope.row.auditStatus == 1" type="text" icon="el-icon-edit" @click="addreturnGoodsOrder(scope.row.id)">修改</el-button>
            <el-button v-if="permissions.eims_returnmanagement_del" :disabled="scope.row.auditStatus == 1" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next"
      ></el-pagination>
    </div>
    <detail-dialog v-if="detailDialogVisible" ref="detailDialog"></detail-dialog>
    <table-form v-if="addOrderVisible" ref="addOrder" @refreshDataList="getDataList"></table-form>
    <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
    <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
  </div>
</template>

<script>
  import TableForm from "./form"
  import DetailDialog from "./detail"
  import { filterParams } from "@/util/tools"
  import { returnManagementFetchList, returnManagementById, delReturnManagementOrder, auditInfoById, getReturnGoodsNumberSum } from "@/api/eims/return-management"
  import { mapGetters } from "vuex"
  import SupplySearch from '@/components/business/SearchSupply'
  import { getCtgDataList } from "@/api/eims/check"
  import SelectBrand from '../storage/SearchBrand'
  import {getStockList} from '@/api/eims/stock'
  import { getDeptInfo } from "@/api/admin/dept"

export default {
  data() {
    return {
      dataForm: {
        supplyId: '',
        supplyName: '',
        brandName: '',
        brandId: ''
      },
      auditStatusOptions: [
        { label: '已审核',value: 1},
        { label: '未审核',value: 0}
      ],
      dataList: [],
      ctgOptions: [],
      deptOptions: [],
      stockOptions: [],
      pageIndex: 1,
      pageSize: 200,
      totalPage: 0,
      dataListLoading: false,
      addOrderVisible: false,
      detailDialogVisible: false,
      supplyVisible: false,
      selectBrandVisible: false,
      sum: ''
    };
  },
  created() {
    this.getDataList();
    this.getGoodsCtgList();
    this.getStock();
    this.getDeptList()
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  components: {
    DetailDialog,
    TableForm,
    SupplySearch,
    SelectBrand
  },
  methods: {
    //  查询数据
    searchHandle() {
      this.pageIndex = 1
      this.getDataList()
    },
    //  清空搜索表单
    clearSearchForm() {
      for (let key in this.dataForm) {
        this.dataForm[key] = ""
      }
    },
    // 获取数据列表
    getDataList() {
      let params1 = { current: this.pageIndex, size: this.pageSize }
      let params2 = filterParams(this.dataForm)
      this.dataListLoading = true
      returnManagementFetchList(Object.assign(params1, params2)).then(response => {
        if(response.data.code == '0'){
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        }else{
          this.$message.error(response.data.msg)
        }
        this.dataListLoading = false
      })
      if(this.dataForm.countShowFlag){
        getReturnGoodsNumberSum(this.dataForm).then(response => {
          if(response.data.code == '0'){
            this.sum = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 查看详情
    returnGoodsDetail(id) {
      this.detailDialogVisible = true
      this.$nextTick(() => {
        this.$refs.detailDialog.init(id)
      })
    },
    auditInfo(id,returnBillId){
      this.$confirm('是否确认审核单据编号为' + returnBillId, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return auditInfoById(id)
      }).then(response => {
        if(response.data.code == '0'){
          this.$message.success('审核成功')
          this.getDataList()
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    // 新增 / 修改
    addreturnGoodsOrder(id) {
      this.addOrderVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrder.init(id);
      });
    },
    // 删除
    deleteHandle (id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delReturnManagementOrder(id)
      }).then(response => {
        if(response.data.code == '0'){
          this.$message.success('删除成功')
          this.getDataList()
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    //  获取仓位列表
    getStock() {
      getStockList().then(response => {
        if(response.data.code == '0'){
          this.stockOptions = response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    //获取部门列表
    getDeptList() {
      getDeptInfo().then(response => {
        if(response.data.code == '0'){
          this.deptOptions = response.data.data
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    changeCtgId(){
      this.dataForm.supplyId = ''
      this.dataForm.supplyName = ''
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    //获取制造商列表
    chooseSupply() {
      let data = {
        ctgId: this.dataForm.ctgId
      }
      this.supplyVisible = true
      this.$nextTick(() => {
        this.$refs.supplySearch.init(data)
      })
    },
    updateSearchForm(data) {
      this.dataForm.supplyId = data.id
      this.dataForm.supplyName = data.name
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    auditStatusFormatter(row, column, cellValue, index) {
      switch (cellValue) {
        case 1:
          return '已审核'
          break
        case 0:
          return '未审核'
          break
      }
    },
    //获取商品类别
    getGoodsCtgList() {
      getCtgDataList().then(response => {
        if(response.data.code == '0'){
          let list = response.data.data.records
          let options = []
          list.forEach(item => {
            options.push({ value: item.id, name: item.name })
          })
          this.ctgOptions = options
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    // 获取商品品种信息
    getBrandList() {
      let data = {
        ctgId: this.dataForm.ctgId,
        supplyId: this.dataForm.supplyId
      }
      this.selectBrandVisible = true
      this.$nextTick(() => {
        this.$refs.selectBrand.init(data)
      })
    },
    //  更新表单品种信息
    updateFormBrand(data) {
      this.dataForm.brandId = data.id
      this.dataForm.brandName = data.name
      this.dataForm.brandCode = data.code
      this.dataForm.name = data.name
      this.dataForm.unitType = data.unitType
    },
  }
};
</script>
